package com.hxzy.research.controller.usermanager;

import com.hxzy.research.pojo.TblTeacher;
import com.hxzy.research.service.pageandmanager.AddTeacherManager;
import com.hxzy.research.service.pageandmanager.FrontTeach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TeacherController {
    @Autowired private FrontTeach frontTeach;
    @Autowired private AddTeacherManager addTeacherManager;
    @RequestMapping("/user/manager/forwardTeacherManager.go")
    public String forwardTeacherManager(Model model, Integer branchid, HttpSession session)throws Exception{
        session.setAttribute("branchid",branchid);
        //查询教师
        List<TblTeacher> teacherList=frontTeach.findTeacherById(branchid);
        model.addAttribute("teacherList",teacherList);
        return "admin/manager/teacher/teacherManager";
    }

    //转发到添加老师页面
    @RequestMapping("/user/manager/forwardAddTeacher.go")
    public String forwardAddTeacher()throws Exception{
        return "/admin/manager/teacher/addTeacher";
    }
    //添加老师
    @RequestMapping("/user/manager/addTeacher.go")
    public @ResponseBody TblTeacher addTeacher(TblTeacher tblTeacher,HttpSession session)throws Exception{
        //执行添加
        tblTeacher.setBranchid((Integer) session.getAttribute("branchid"));
        addTeacherManager.addTeacher(tblTeacher);
        return tblTeacher;
    }

    //转发到修改老师页面
    @RequestMapping("/user/manager/forwardUpdateTeacher.go")
    public String forwardUpdateTeacher(Model model,Integer tid,HttpSession session) throws Exception{
        session.setAttribute("tid",tid);
        //通过老师id查老师
        TblTeacher tblTeacher=addTeacherManager.findTeacherById(tid);
        model.addAttribute("teacher",tblTeacher);
        return "/admin/manager/teacher/updateTeacher";
    }
    //修改老师
    @RequestMapping("/user/manager/updateTeacher.go")
    public @ResponseBody TblTeacher updateTeacher(TblTeacher tblTeacher,HttpSession session) throws Exception{
        //执行修改
        tblTeacher.setId((Integer) session.getAttribute("tid"));
        tblTeacher.setBranchid((Integer) session.getAttribute("branchid"));
        addTeacherManager.updateTeacher(tblTeacher);
        return tblTeacher;
    }
    //删除老师
    @RequestMapping("/user/manager/deleteTeacher.go")
    public String deleteTeacher(Integer tid,HttpSession session)throws Exception{
        //执行删除
        addTeacherManager.deleteTeacher(tid);
        Integer branchid=(Integer) session.getAttribute("branchid");
        return "redirect:/user/manager/forwardTeacherManager.go?branchid="+branchid;
    }
}
